<?php

namespace App\Http\Controllers;

use App\Models\Cates;
use App\Models\Follows;
use App\Models\FormIds;
use Carbon\Carbon;
use Illuminate\Http\Request;

/**
 * 关注相关
 * Class FollowController
 * @package App\Http\Controllers
 */
class FollowController extends Controller{

    /**
     * 关注/取消关注分类
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function index(Request $request){
//        try {
            $id = (int)$request->get('id');
            if(empty($id)){
                return $this->returnJson(1,'参数错误');
            }
            $info = Cates::find($id);
            if(!$info){
                return $this->returnJson(1,'数据不存在');
            }
            $userid = auth()->guard('api')->id();
            $follow = Follows::where('cate_id',$id)->where('userid',$userid)->first();
            if($follow){
                //取消关注
                $follow->delete();
                return $this->returnJson(0,'取消关注');
            }else{
                //关注
                $insert = [
                    'created_at' => Carbon::now(),
                    'cate_id'    => $id,
                    'userid'     => $userid,
                ];
                Follows::create($insert);
                return $this->returnJson(0,'关注成功');
            }
//        } catch (\Exception $e) {
//            return $this->returnJson(1, '网络超时，请刷新再试');
//        }
    }

}
